package runningQueen.se_bigwork;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JSlider;
import javax.swing.plaf.metal.MetalSliderUI;

/**
 * 自定义的拖动条，适合用来调节音量、音效
 *
 */
public class MySlider extends JSlider{
	
	protected MySliderUI ui = null;
	private Image thumbImg = null;
	
	/**
	 * 不使用用户图片，经过美化的拖动条
	 * @param min 最小值
	 * @param max 最大值
	 * @param value 初始值
	 */
	public MySlider(int min, int max, int value){
		this(min, max, value, null);
	}

	/**
	 * 使用用户自定义图片的拖动条，但是还不够通用，比如长度和大小还无法自适应啥的<br />
	 * 而且也不知道能不能自定义的拖动条图片【这才是重点2333
	 * @param min 最小值
	 * @param max 最大值
	 * @param value 初始值
	 * @param thumbImg 指示物的图标，没有则用null表示
	 */
	public MySlider(int min, int max, int value, Image thumbImg){
		super(min, max, value);
		ui = new MySliderUI(thumbImg);
		setUI(ui);
		initSlider();
	}
	
	private void initSlider(){
		// 设定为显示
		setPaintTicks(true);
		setPaintLabels(true);
		if (thumbImg == null) {
			addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					ui.setThumbColorDarker();
				};
				@Override
				public void mouseReleased(MouseEvent e) {
					ui.setThumbColorBrighter();
				};
			});
		} else {
			addMouseListener(new MouseAdapter() {
			});
		}
	}
	
	public void setThumbColor(Color c){
		ui.setThumbColor(c);
	}
}

class MySliderUI extends MetalSliderUI {
	
	Image thumbImg = null;
	Color thumbColor = Color.BLACK;
	
	MySliderUI(){
	}
	
	MySliderUI(Image thumbImg){
		this.thumbImg = thumbImg;
	}
	
	public void setThumbColor(Color c){
		thumbColor = c;
	}
	
	public void setThumbColorDarker(){
		thumbColor = thumbColor.darker().darker().darker();
	}
	
	public void setThumbColorBrighter(){
		thumbColor = thumbColor.brighter().brighter().brighter();
	}
	/**
	 * 绘制指示物，注释掉效果比较好，但是默认的好难看……<br />
	 * 建议用图片……虽然还没完成
	 */
//	TODO图片
	public void paintThumb(Graphics g) {
		// 不使用指示图片
		Graphics2D g2d = (Graphics2D) g;
		if (thumbImg == null) {
			g2d.setColor(thumbColor);
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			// 填充椭圆框为当前thumb位置
			g2d.fillOval(thumbRect.x, thumbRect.y, thumbRect.width,
					thumbRect.height);
		} else {
			// 贴图的方式(利用鼠标事件转换image为不同状态)
			g2d.drawImage(thumbImg, thumbRect.x, thumbRect.y-4,
				thumbRect.width,thumbRect.height+4,null);
		}
	}
	
	/**
	 * 绘制滑动条
	 * @param g
	 */
	public void paintTrack(Graphics g) {
		int cy, cw;// 指示物跑的那个线的图片位置
		Rectangle trackBounds = trackRect;
		if (slider.getOrientation() == JSlider.HORIZONTAL) {
			
			BufferedImage image = new BufferedImage(trackBounds.width+30, trackBounds.height,
			                BufferedImage.TYPE_INT_ARGB);
			
			Graphics2D g2 = image.createGraphics();

			cy = (trackBounds.height / 2) ;
			cw = trackBounds.width;

			makeBaseTrack(g2, g.getColor(), cy, cw, trackBounds);
			makeColorTrack(g2, cy, cw);
			makeMaskTrack(g2, cy, cw);
			
			g2.dispose();
			g.drawImage(image, 0, 0, null);
			
		} else {
			super.paintTrack(g);
		}
	}

	/**
	 * 上方三角形的遮罩层，被遮罩的部分会显示透明
	 * @param g2
	 * @param cy
	 * @param cw
	 */
	private void makeMaskTrack(Graphics2D g2, int cy, int cw) {
		Polygon polygon = new Polygon();
		polygon.addPoint(0, cy);
		polygon.addPoint(0, -cy);
		polygon.addPoint(cw, -cy);

		g2.setComposite(AlphaComposite.SrcOut);
		g2.setColor(Color.black);
		g2.fillPolygon(polygon);

		polygon.reset();
	}

	/**
	 * 滑动条上方的颜色，游标左边的部分
	 * @param g2
	 * @param cy
	 * @param cw
	 */
	private void makeColorTrack(Graphics2D g2, int cy, int cw) {
		int trackLeft = 0;
		int trackRight = 0;
		trackRight = trackRect.width - 1;
		int middleOfThumb = 0;
		int fillLeft = 0;
		int fillRight = 0;

		// 坐标换算
		middleOfThumb = thumbRect.x + (thumbRect.width / 2);
		middleOfThumb -= trackRect.x;

		if (!drawInverted()) {
			fillLeft = !slider.isEnabled() ? trackLeft
					: trackLeft + 1;
			fillRight = middleOfThumb;
		} else {
			fillLeft = middleOfThumb;
			fillRight = !slider.isEnabled() ? trackRight - 1
					: trackRight - 2;
		}
		// 设定渐变
		g2.setPaint(new GradientPaint(0, 0, new Color(0, 100,
				100), cw, 0, new Color(0, 255, 100),
				true));
		g2.fillRect(0, -cy, fillRight - fillLeft, cy * 2);
	}

	/**
	 * 滑动条的最底层
	 * @param g2
	 * @param color
	 * @param cy
	 * @param cw
	 * @param trackBounds
	 */
	private void makeBaseTrack(Graphics2D g2, Color color, int cy, int cw,
			Rectangle trackBounds) {
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.translate(trackBounds.x, trackBounds.y + cy);

		// 背景色
		g2.setColor(color);
		g2.fillRect(0, -cy, cw, cy * 2);
	}
}